package com.gs.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gs.action.datamodel.UserDataModel;
import com.gs.bean.User;
import com.gs.dao.UserDAO;
import com.liang.ext.bean.ExtFormBean;
import com.liang.log.Log;

@Scope(value = "prototype")
@Controller(value = "userAction")
public class UserAction extends BasicAction {
	@Resource
	private UserDAO userDAOImpl;

	private UserDataModel dataModel;

	private List<User> users;


	@Override
	public String execute() throws Exception {
		UserDataModel searchModel = dataModel;
		if (dataModel == null) {
			searchModel = new UserDataModel();
		}
		users = userDAOImpl.queryPage(searchModel, searchModel.getCurrentPage(),
				searchModel.getPageSize()).getList();
		return "SYS.MANAGER.USER.EXECUTE.SUCCESS";
	}

	public String save() throws Exception {
		try {
			User newUser = dataModel.getUser();
			
			userDAOImpl.saveModel(newUser);
			
			//查找当前插入的User对象
			UserDataModel searchModel = dataModel;
			newUser = userDAOImpl.queryPage(searchModel,searchModel.getCurrentPage(),searchModel.getPageSize()).getList().get(0);
			ExtFormBean extform = new ExtFormBean();
			extform.setSuccess(true);
			extform.setMsg("新增成功");
			extform.setResult(newUser);
			writeExtFormBean(extform);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.info("新增成功");
		return null;//"SYS.MANAGER.USER.ADD.SUCCESS";
	}

	public String delete() throws Exception {
		return "SYS.MANAGER.USER.ADD.SUCCESS";
	}

	public String update() throws Exception {
		return "SYS.MANAGER.USER.ADD.SUCCESS";
	}

	public String load() {

		return null;
	}

	@JSON
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public UserDataModel getDataModel() {
		return dataModel;
	}

	public void setDataModel(UserDataModel dataModel) {
		this.dataModel = dataModel;
	}
}
